package com.wp.backup.controller;

import com.wp.backup.constants.BackupConstants;
import com.wp.backup.properties.BackupDataSourceProperties;
import com.wp.backup.service.MySqlBackupService;
import com.wp.backup.vo.Backup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wp
 * @create 2019-03-06 11:53
 */
@RestController
@RequestMapping("backup")
public class MySqlBackupController {

    @Autowired
    private MySqlBackupService mySqlBackupService;
    @Autowired
    private BackupDataSourceProperties properties;

    @GetMapping("/backup")
    public ResponseEntity<Boolean> backup() {
        String host = properties.getHost();
        String userName = properties.getUserName();
        String password = properties.getPassword();
        List<String> databases = properties.getDatabase();
        String backupFodlerName = BackupConstants.DEFAULT_BACKUP_NAME + "_" + (new SimpleDateFormat(BackupConstants.DATE_FORMAT)).format(new Date());
        String backupFolderPath = BackupConstants.BACKUP_FOLDER + backupFodlerName + File.separator;
        String fileName = BackupConstants.BACKUP_FILE_NAME;
        try {
            mySqlBackupService.backup(host, userName, password, backupFolderPath, fileName, databases);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(false);
        }
        return ResponseEntity.ok(true);
    }

    @GetMapping("/restore")
    public ResponseEntity<Void> restore(@RequestParam String name) {
        String host = properties.getHost();
        String userName = properties.getUserName();
        String password = properties.getPassword();
        List<String> databases = properties.getDatabase();
        String restoreFilePath = BackupConstants.RESTORE_FOLDER + name;
        try {
            mySqlBackupService.restore(restoreFilePath, host, userName, password, databases);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok().build();
    }

    @GetMapping("/findRecords")
    public ResponseEntity<List<Backup>> findBackupRecords() {
        List<Backup> backupRecords = new ArrayList<>();
        File restoreFolderFile = new File(BackupConstants.RESTORE_FOLDER);
        if (restoreFolderFile.exists()) {
            for (File file : restoreFolderFile.listFiles()) {
                Backup bean = new Backup();
                bean.setName(file.getName());
                bean.setTitle(file.getName());
                if (BackupConstants.DEFAULT_BACKUP_NAME.equals(file.getName())) {
                    bean.setTitle("系统默认备份");
                }
                backupRecords.add(bean);
            }
        }
        return ResponseEntity.ok(backupRecords);
    }

}
